package model;

public class InsertionSort extends Sorter {

    public InsertionSort() {
	super();
	statistics = new Statistics();
    }

    @SuppressWarnings("unchecked")
    public void sort(Comparable[] workArray) {
	long beforeTime = System.nanoTime();
	
	for (int i = 1; i < workArray.length; i++) {
	    /* Insert a[i] into the sorted sublist */
	    Comparable insert = workArray[i];
	    int j;
	    for (j = i - 1; j >= 0; j--) {
		// Check
		statistics.incrementChecks();

		if (workArray[j].compareTo(insert) <= 0)
		    break;
		
		workArray[j + 1] = workArray[j];
	         // Swap
		 //-----------------
                if(simulation)
                PushSwapToQueue(j+1, j);
                //-----------------
		statistics.incrementSwaps();
	    }
	    workArray[j + 1] = insert;
	}
	
	long afterTime = System.nanoTime();
	statistics.setRunTime(afterTime - beforeTime);
    }

    public int getComplexity(int arraySize) {
	return (int) Math.pow(arraySize, 2);
    }

}
